<?php

namespace common\models\wechat;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "wechat_menu".
 *
 * @property string $id
 * @property string $name
 * @property integer $displayorder
 * @property string $type
 * @property integer $parentid
 */
class WechatMenu extends \yii\db\ActiveRecord
{



    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '微信菜单',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];

    //连接事件
    const BTNTYPE_VIEW = 'view';

    //点击事件
    const BTNTYPE_CLICK = 'click';

    //扫码推事件
    const BTNTYPE_SCANCODE_PUSH = 'scancode_push';

    //扫码推事件且弹出“消息接收中”提示框
    const BTNTYPE_SCANCODE_WAITING = 'scancode_waitmsg';

    //弹出系统拍照发图
    const BTNTYPE_PIC_SYSPHOTO = 'pic_sysphoto';

    //弹出拍照或者相册发图
    const BTNTYPE_PIC_PHOTO_OR_ALBUM  = 'pic_photo_or_album';

    //弹出微信相册发图器
    const BTNTYPE_PIC_WEIXIN = 'pic_weixin';

    //弹出地理位置选择器
    const BTNTYPE_LOCATION_SELECT = 'location_select';


    public static $btntypes = [
            self::BTNTYPE_VIEW => '链接',
            self::BTNTYPE_CLICK => '点击',
            self::BTNTYPE_SCANCODE_PUSH => '扫码推',
            self::BTNTYPE_SCANCODE_WAITING => '扫码推事件且弹出“消息接收中”提示框',
            self::BTNTYPE_PIC_SYSPHOTO => '弹出系统拍照发图',
            self::BTNTYPE_PIC_PHOTO_OR_ALBUM => '弹出拍照或者相册发图',
            self::BTNTYPE_PIC_WEIXIN => '弹出微信相册发图器',
            self::BTNTYPE_LOCATION_SELECT => '弹出地理位置选择器',
        ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'wechat_menu';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'displayorder', 'type'], 'required'],
            [['displayorder', 'parentid'], 'integer'],
            [['name', 'type','trigger'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '菜单名',
            'displayorder' => '排序',
            'type' => '菜单点击事件类型',
            'parent' => '上级菜单',
            'trigger' => '内容',
            'parentid' => '上级菜单',
        ];
    }



    //获取父级菜单
    public function getParent(){
        return $this->hasOne(self::className(),['id'=>'parentid']);
    }



    //获取顶级菜单
    public function findTopMenu()
    {
        $tops = WechatMenu::find()->select('*')->where('parentid=0')->asArray()->all();
        $tops = array_merge([['id'=>0,'name'=>'顶级菜单']],$tops);
        return ArrayHelper::map($tops,'id','name');

    }


}
